<?php
/**
 * Created by PhpStorm
 * User: Jhinwy
 * Date: 5/20/24
 * Time: 3:21 PM
 */

namespace App\Http\Controllers\Admin;

use App\Http\Resources\AdminResource;
use Illuminate\Http\Request;
use App\Services\AdminService;
use App\Http\Controllers\ResourceController;

class AdminController extends ResourceController
{
    protected $service;

    public function __construct(AdminService $service)
    {
        $this->service = $service;
    }


    /**
     * 管理员登录
     *
     * @param Request $request
     * @return mixed
     */
    public function login(Request $request)
    {
        $params = $request->post();
        $result = $this->service->login($params);
        if (is_string($result)) {

            return $this->responseWithError($result);
        }

        return $this->responseWithSuccess($result);
    }



    /**
     * 管理员列表
     *
     * @param Request $request
     * @return mixed
     */
    public function getList(Request $request)
    {
        return $this->responseWithSuccess(
            AdminResource::collection($this->service->getList($request->post()))->resource
        );
    }


    /**
     * 获取所有管理员账号信息
     *
     * @param Request $request
     * @return mixed
     */
    public function getAll(Request $request)
    {
        return $this->responseWithSuccess($this->service->getAll());
    }


    /**
     * 获取管理员详情
     *
     * @param Request $request
     * @return mixed
     */
    public function detail(Request $request)
    {
        return $this->responseWithSuccess($this->service->detail());
    }


    /**
     * 新建管理员
     *
     * @param Request $request
     * @return mixed
     */
    public function create(Request $request)
    {
        $params   = $request->post();

        return $this->service->create($params) ?
            $this->responseWithSuccess() :
            $this->responseWithError('失败');
    }


    /**
     * 删除管理员
     *
     * @param Request $request
     * @return mixed
     */
    public function delete(Request $request)
    {
        return $this->service->delete($request->post('id', '')) ?
            $this->responseWithSuccess() :
            $this->responseWithError('删除失败');
    }


    /**
     * 管理员登出
     *
     * @param Request $request
     * @return mixed
     */
    public function logout(Request $request)
    {
        $this->service->logout();

        return $this->responseWithSuccess();
    }
}
